<template>
  <a-divider>{{ $t("136") }}</a-divider>
  <a-descriptions bordered :column="1">
    <a-descriptions-item :label="$t('133')">{{
      statistics.version
    }}</a-descriptions-item>
    <a-descriptions-item :label="$t('137')">
      {{ formatSeconds(statistics.upSeconds) }}</a-descriptions-item
    >
    <a-descriptions-item :label="$t('138')">{{
      statistics.userCount
    }}</a-descriptions-item>
    <a-descriptions-item :label="$t('139')">{{
      statistics.modelCount
    }}</a-descriptions-item>
    <a-descriptions-item :label="$t('140')">
      {{ statistics.allRequestCount }}
    </a-descriptions-item>
  </a-descriptions>
  <a-divider>系统管理员组</a-divider>
  <a-transfer
    v-model:target-keys="targetKeys"
    :data-source="appStore.otherUsers"
    :titles="['User', 'Administrator']"
    :render="(item) => item.name"
    @change="handleChange"
    :showSelectAll="false"
  />
</template>
<script setup>
import { computed, onMounted, ref } from "vue";
import CommonDefine from "@/component/CommonDefine";

const statistics = ref({});
const appStore = CommonDefine.appStore();

function formatSeconds(seconds) {
  let r = "";
  if (seconds > 86400) {
    let days = Math.floor(seconds / 86400);
    seconds = seconds % 86400;
    r += days + "天";
  }
  if (seconds > 3600) {
    let hours = Math.floor(seconds / 3600);
    seconds = seconds % 3600;
    r += hours + "时";
  }
  if (seconds > 60) {
    let minutes = Math.floor(seconds / 60);
    seconds = seconds % 60;
    r += minutes + "分";
  }
  r += seconds + "秒";
  return r;
}

const getStatistics = () => {
  CommonDefine.apiGetArm("/api-admin/statistics", (d) => {
    statistics.value = d;
  });
};

const targetKeys = computed(() =>
  appStore.otherUsers.filter((x) => x.role == "UrAdmin").map((x) => x.key)
);

const handleChange = (nextTargetKeys, direction, moveKeys) => {
  CommonDefine.apiPostArm(
    "/api-admin/admin-role",
    {
      uIds: moveKeys,
      flag: direction == "right",
    },
    (d) => {
      moveKeys.forEach((x) => {
        appStore.otherUsers.value
          .filter((u) => u.key == x)
          .forEach((u) => {
            u.role = direction == "right" ? "UrAdmin" : "UrUser";
          });
      });
    }
  );
};

onMounted(() => {
  getStatistics();
});
</script>
